package org.tinygroup.springmvc.negotiation.impl;

import java.util.Map;

import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest;
import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;

/**
 * A ContentNegotiationStrategy that uses a request parameter to determine what
 * media types are requested. The default parameter name is {@code format}. Its
 * value is used to look up the media type in the map given to the constructor.
 *
 * @author renhui
 */
public class ParameterContentNegotiationStrategy extends
		AbstractMappingContentNegotiationStrategy {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ParameterContentNegotiationStrategy.class);

	private String parameterName = "format";

	/**
	 * Create an instance with the given extension-to-MediaType lookup.
	 * 
	 * @throws IllegalArgumentException
	 *             if a media type string cannot be parsed
	 */
	public ParameterContentNegotiationStrategy(Map<String, MediaType> mediaTypes) {
		super(mediaTypes);
	}

	/**
	 * Set the parameter name that can be used to determine the requested media
	 * type.
	 * <p>
	 * The default parameter name is {@code format}.
	 */
	public void setParameterName(String parameterName) {
		Assert.notNull(parameterName, "parameterName is required");
		this.parameterName = parameterName;
	}

	@Override
	protected String getMediaTypeKey(NativeWebRequest webRequest) {
		return webRequest.getParameter(this.parameterName);
	}

	@Override
	protected void handleMatch(String mediaTypeKey, MediaType mediaType) {
		LOGGER.logMessage(
				LogLevel.DEBUG,
				"Requested media type is '{0}' (based on parameter '{1}'='{2}')",
				mediaType, this.parameterName, mediaTypeKey);
	}

	@Override
	protected MediaType handleNoMatch(NativeWebRequest request, String key)
			throws HttpMediaTypeNotAcceptableException {
		throw new HttpMediaTypeNotAcceptableException(getAllMediaTypes());
	}
}
